using System;
using Serilog;
using Serilog.Core;
using Serilog.Events;

namespace SqlInjectionScanner.Utils
{
    /// <summary>
    /// 日志工具类
    /// </summary>
    public static class Logger
    {
        private static ILogger? _logger;

        /// <summary>
        /// 初始化日志记录器
        /// </summary>
        /// <param name="logFilePath">日志文件路径</param>
        /// <param name="minLevel">最小日志级别</param>
        public static void Initialize(string logFilePath, LogEventLevel minLevel = LogEventLevel.Information)
        {
            _logger = new LoggerConfiguration()
                .MinimumLevel.Is(minLevel)
                .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
                .WriteTo.File(logFilePath, 
                    rollingInterval: RollingInterval.Day,
                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
                .CreateLogger();

            Information("日志系统初始化完成");
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="messageTemplate">消息模板</param>
        /// <param name="propertyValues">属性值</param>
        public static void Debug(string messageTemplate, params object[] propertyValues)
        {
            _logger?.Debug(messageTemplate, propertyValues);
        }

        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="messageTemplate">消息模板</param>
        /// <param name="propertyValues">属性值</param>
        public static void Information(string messageTemplate, params object[] propertyValues)
        {
            _logger?.Information(messageTemplate, propertyValues);
        }

        /// <summary>
        /// 记录警告
        /// </summary>
        /// <param name="messageTemplate">消息模板</param>
        /// <param name="propertyValues">属性值</param>
        public static void Warning(string messageTemplate, params object[] propertyValues)
        {
            _logger?.Warning(messageTemplate, propertyValues);
        }

        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="messageTemplate">消息模板</param>
        /// <param name="propertyValues">属性值</param>
        public static void Error(string messageTemplate, params object[] propertyValues)
        {
            _logger?.Error(messageTemplate, propertyValues);
        }

        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="exception">异常</param>
        /// <param name="messageTemplate">消息模板</param>
        /// <param name="propertyValues">属性值</param>
        public static void Error(Exception exception, string messageTemplate, params object[] propertyValues)
        {
            _logger?.Error(exception, messageTemplate, propertyValues);
        }

        /// <summary>
        /// 记录致命错误
        /// </summary>
        /// <param name="messageTemplate">消息模板</param>
        /// <param name="propertyValues">属性值</param>
        public static void Fatal(string messageTemplate, params object[] propertyValues)
        {
            _logger?.Fatal(messageTemplate, propertyValues);
        }

        /// <summary>
        /// 记录致命错误
        /// </summary>
        /// <param name="exception">异常</param>
        /// <param name="messageTemplate">消息模板</param>
        /// <param name="propertyValues">属性值</param>
        public static void Fatal(Exception exception, string messageTemplate, params object[] propertyValues)
        {
            _logger?.Fatal(exception, messageTemplate, propertyValues);
        }
    }
}